package gui;

import java.util.Calendar;

import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;

import logic.FilesManager;
import logic.LogicHelper;
import logic.MainMidlet;
import logic.Point;

/**
 * screen for loading game
 * @author Rekin
 *
 */
public class LoadGameScreen extends Canvas{
	private long fileDate;
	private String filedateText;
	private CanvasButton[] okCancel;
	
	/**
	 * default constructor
	 */
	public LoadGameScreen(){
		fileDate=FilesManager.getSavedGameDate();
		if(fileDate>0){
			fileDate = Calendar.getInstance().getTime().getTime() - FilesManager.getSavedGameDate();
			if(fileDate>0)
				filedateText = "Created: "+fileDate/1000+"s ago.";
			else
				filedateText = "Creation time: Unknown";
		}
		else{
			filedateText=null;
		}
		okCancel=new CanvasButton[2];
		if(!Board.deviceBuild){
			okCancel[0] = new CanvasButton(70, 25, new Point(getWidth()/2-80, getHeight()/2+10), "Load");
			okCancel[1] = new CanvasButton(70, 25, new Point(getWidth()/2+10, getHeight()/2+10), "Cancel");
		}
		else{
			okCancel[0] = new CanvasButton(90, 45, new Point(getWidth()/2-100, getHeight()/2+10), "Load");
			okCancel[1] = new CanvasButton(90, 45, new Point(getWidth()/2+10, getHeight()/2+10), "Cancel");
		}
		if(filedateText!=null){
			okCancel[0].setEnabled(true);
		}
		else{
			okCancel[0].setEnabled(false);
		}
	}
	
	/**
	 * additional paintings
	 */
	protected void paint(Graphics g) {
		g.setColor(0,0,0);
		g.fillRect(0, 0, getWidth(), getHeight());	 //black background
		g.setColor(200,50,50);
		if(filedateText==null){
			g.drawString("No game to load.", getWidth()/2, getHeight()/3, Graphics.BASELINE | Graphics.HCENTER);
		}
		else{
			g.drawString("Load game from date:", getWidth()/2, getHeight()/3, Graphics.BASELINE | Graphics.HCENTER);
			g.drawString(filedateText, getWidth()/2, getHeight()/3+25, Graphics.BASELINE | Graphics.HCENTER);
		}
		okCancel[0].paintMe(g);
		okCancel[1].paintMe(g);
	}
	
	/**
	 * handles button hit
	 * @param button pressed button
	 */
	private void checkButtonHit(int button){
		if(button==0){ //load
			if(!okCancel[0].isEnabled()){
				return;
			}
			loadButtonEvent();
		}
		else if(button==1){ //cancel
			MainMidlet.getMidletInstance().showMainMenu();
		}
	}
	
	/**
	 * loading event
	 */
	private void loadButtonEvent(){
		MainMidlet.getMidletInstance().prepareAndShowLoadedGame();
	}
	
	/**
	 * finds button that was hit depending on press coordinates
	 * @param x horizontal coordinate hit
	 * @param y vertical coordinate hit
	 * @return pressed button index or -1
	 */
	private int buttonHit(int x, int y){
		for(int i=0;i<okCancel.length;++i){
			int ex1=okCancel[i].getTopLeft().getX();
			int ex2=ex1 + okCancel[i].getMyWidth();
			int ey1=okCancel[i].getTopLeft().getY();
			int ey2=ey1 + okCancel[i].getMyHeight();
			
			if(x>=ex1 && x<=ex2 && y>=ey1 && y<=ey2){
				return i;
			}
		}
		return -1;
	}
	
	/**
	 * press event from canvas
	 */
	protected void pointerPressed(int x, int y){
		checkButtonHit(buttonHit(x,y));
	}

}
